/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.live.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.wx.WxInterfaceUtil;
import com.yami.shop.common.wx.bean.PageLiveRoomInfo;
import com.yami.shop.common.wx.bean.WxLiveRoomInfo;
import com.yami.shop.config.WxConfig;
import com.yami.shop.live.comment.model.LiveRoom;
import com.yami.shop.live.comment.service.LiveRoomService;
import com.yami.shop.common.annotation.SysLog;

import javax.validation.Valid;

import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.common.util.PageParam;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Objects;


/**
 *
 *
 * @author lhd
 * @date 2020-08-05 08:53:17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/live/liveRoom" )
public class LiveRoomController {

    private final LiveRoomService liveRoomService;
    private final WxConfig wxConfig;
    private final WxInterfaceUtil wxInterfaceUtil;

    /**
     * 分页查询
     * @param page 分页对象
     * @param liveRoom
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<LiveRoom>> getLiveRoomPage(PageParam<LiveRoom> page, LiveRoom liveRoom){
        return ResponseEntity.ok(liveRoomService.page(page, new LambdaQueryWrapper<LiveRoom>()
                .like(StrUtil.isNotBlank(liveRoom.getName()),LiveRoom::getName,liveRoom.getName())
                .like(StrUtil.isNotBlank(liveRoom.getAnchorName()),LiveRoom::getAnchorName,liveRoom.getAnchorName())
                .eq(Objects.nonNull(liveRoom.getLiveStatus()),LiveRoom::getLiveStatus,liveRoom.getLiveStatus())
                .eq(Objects.nonNull(liveRoom.getRoomTop()),LiveRoom::getRoomTop,liveRoom.getRoomTop())
                .ge(Objects.nonNull(liveRoom.getStartTime()),LiveRoom::getStartTime,liveRoom.getStartTime())
                .le(Objects.nonNull(liveRoom.getStartTime()),LiveRoom::getStartTime,liveRoom.getEndTime())
                .orderByDesc(LiveRoom::getStartTime)
        ));
    }


    /**
     * 通过id查询
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}" )
    public ResponseEntity<LiveRoom> getById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(liveRoomService.getById(id));
    }

    /**
     * 通过id是否置顶
     * @param id id
     * @return 是否置顶成功
     */
    @PutMapping("/toTop/{id}" )
    @PreAuthorize("@pms.hasPermission('live:liveRoom:update')" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long id) {
        LiveRoom liveRoom = liveRoomService.getById(id);
        int count = liveRoomService.count(new LambdaQueryWrapper<LiveRoom>().eq(LiveRoom::getRoomTop, 1));
        if(count >= Constant.MAX_TOP_NUM && Objects.equals(liveRoom.getRoomTop(),0)){
            // 置顶已达上限10个无法进行置顶，请先取消其余直播间的置顶
            throw new YamiShopBindException("yami.live.room.top.check");
        }
        liveRoom.setRoomTop(Objects.equals(liveRoom.getRoomTop(),0) ? 1:0);
        return ResponseEntity.ok(liveRoomService.updateById(liveRoom));
    }

}
